package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import data.BankDatabaseUserSQL;

/** Represents a jtable of the back end sql database.
 * The purpose of the ui, is to have a fully functional
 * sql command prompt on the front end, allowing the admin
 * to enter SQL syntax, in order to retrieve data from the database
 * @author Matthias
 */
public class AdmView extends JPanel {
	
	/** button to get back **/
	private final JButton btnBack;
	/** jtextfield to enter a query in **/
	private final JTextField textFieldInputQuery;                      
	/** The table for displaying data **/
	private final JTable table;                 
	/**For displaying messages **/
	private final JLabel lblMsgLine;
	/** declaring the object **/
	private final BankDatabaseUserSQL obtainData;

	/** Constructor of AdmView
	 * @param obtainData
	 */
	public AdmView(final BankDatabaseUserSQL obtainData) {

		this.obtainData = obtainData;

		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(600, 480));
		
		table = new JTable();
		table.setFont(new Font("SansSerif", Font.PLAIN, 12));
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		add(new JScrollPane(table), BorderLayout.CENTER);

		textFieldInputQuery = new JTextField(); 
		textFieldInputQuery.setFont(new Font("SansSerif", Font.PLAIN, 16));
		add(textFieldInputQuery, BorderLayout.NORTH);

		lblMsgLine = new JLabel();
		add(lblMsgLine, BorderLayout.SOUTH);

		btnBack = new JButton("Back");
		btnBack.setFont(new Font("SansSerif", Font.PLAIN, 16));
		add(btnBack, BorderLayout.PAGE_END);

		textFieldInputQuery.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				try {
					displayQueryResults(textFieldInputQuery.getText());
					clearField();
				} catch(Exception exc) {
					JOptionPane.showMessageDialog(null, "No valid command! Please enter" +
						" a right command!", "Error!", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}
	
	/**
	 * clearing the field in the jtextfield 
	 * after an action
	 */
	public void clearField() {
		textFieldInputQuery.setText("");
	}
	
	/**
	 * Inserting a default SQL syntax, when the admin enteres
	 * this gui. By pressing the enter, it performs the action.
	 * The purpose of this method is to show the admin, to use
	 * SQL syntax, to retrieving data from the database
	 */
	public void startCommand() {
		textFieldInputQuery.setText("SELECT * FROM db_user");
	}
	
	/** adding actionlistener to btnBack
	 * @param e
	 */
	public void addActionListener(final ActionListener e){
		btnBack.addActionListener(e);
	}

	/** 
	 * @return button back
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/** Creating a tablemodel with the query string. The Tablemodel contains the database query.
	 * In fact, this method displays all data from the database
	 * @param q
	 */
	public void displayQueryResults(final String q) {
		try {
			// displaying the query in the jtable
			table.setModel(obtainData.viewDB(q));
		}
		catch (Exception exc) {}
	}

} // end class AdmView